package com.test.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
//import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.test.dto.Document;
import com.test.dto.Result;
import com.test.mapper.TestMapper;
import com.test.service.TestService;
import com.test.util.IpUtil;
import lombok.Data;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author 41636
 */
@RestController
public class ClickInfoController {

    @Resource
    private TestService testService;

    @GetMapping("/info")
    public String recordClickInfo(HttpServletRequest request) {
        String ipAddr = IpUtil.getIpAddr(request);
        System.out.println(ipAddr);
        return "您的点击信息已被记录";
    }

    @Autowired
    private TestMapper testMapper;

    @GetMapping("/insert")
    public String insertClickInfo() {
        List<Document> documents = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Document document = new Document();
            document.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            document.setTitle("title:" + UUID.randomUUID());
            document.setContent("content:" + UUID.randomUUID());
            documents.add(document);
        }

        for (int i = 0; i < 100; i++) {
            Document document = new Document();
            document.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            document.setTitle("title:" + UUID.randomUUID());
            documents.add(document);
        }
        testMapper.batchInsert(documents);
        return "插入成功";
    }

    @GetMapping("/get")
    public Result getClickInfo() {
        List<Document> documents = testService.findAll();
        return Result.ok(documents);
    }

    @GetMapping("/get1")
    public Result get1() {
        return Result.ok(testService.get1());
    }

    @GetMapping("/letCpuBusy")
    public void letCpuBusy(){
        int i=0;
        while(true) {
            i++;
        }
    }

    @GetMapping("/oom")
    public String oom() {
        List<Byte[]> list = new ArrayList<>();
        int count = 0;
        while (true) {
            //每次增加10M的内存空间
            list.add(new Byte[1024 * 1024]);
            count++;
            System.out.println(count);
        }
    }

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("rockemq")
    public String rockemq() {
        rocketMQTemplate.send("test-topic:one", MessageBuilder.withPayload("hello world").build());
        return "ok";
    }
    @GetMapping("/query")
    public String queryNetWork() {
        String json = "{\n" +
                "\"status\":\"success\",\n" +
                "\"data\":{\n" +
                "\t\"resultType\": \"vector\",\n" +
                "\t\"result\": [{\n" +
                "\t\t\"metric\": {\n" +
                "\t\t\t\"__name__\": \"probe_duration_seconds\",\n" +
                "\t\t\t\"group\": \"icmpaudio\",\n" +
                "\t\t\t\"instance\": \"101.68.85.254\",\n" +
                "\t\t\t\"job\": \"icmp_status\",\n" +
                "\t\t\t\"name\": \"公司-防火墙-联通宽带网络IP\"\n" +
                "\t\t},\n" +
                "\t\t\"value\": [1.67331657775E9, \"0.078213297\"]\n" +
                "\t},{\n" +
                "\t\t\"metric\": {\n" +
                "\t\t\t\"__name__\": \"probe_duration_seconds\",\n" +
                "\t\t\t\"group\": \"icmpaudio\",\n" +
                "\t\t\t\"instance\": \"101.68.85.254\",\n" +
                "\t\t\t\"job\": \"icmp_status\",\n" +
                "\t\t\t\"name\": \"服务测试医院\"\n" +
                "\t\t},\n" +
                "\t\t\"value\": [1.67331657775E9, \"0.934473235\"]\n" +
                "\t}\n" +
                "     ]\n" +
                "}\n" +
                "}";
        return json;
    }

    public static void main(String[] args) {
        String filePath = "D:\\工作簿2.xlsx";


    }

    private List<CommonTValue> readExcel(String filePath) {
        ExcelReader reader = ExcelUtil.getReader(filePath);
        List<Map<String, Object>> readAll = reader.readAll();
        List<CommonTValue> commonTValueList = readAll.stream().map(map -> {
            CommonTValue commonTValue = new CommonTValue();
            commonTValue.setElec(new BigDecimal(map.get("电量（兆瓦时）").toString()));
            commonTValue.setAppliedTime(DateUtil.toLocalDateTime(DateUtil.parseDate(map.get("年月").toString())));
            System.out.println(commonTValue);
            return commonTValue;
        }).collect(Collectors.toList());
        return commonTValueList;
    }

    @Data
    class CommonTValue {
        private static final long serialVersionUID = 1L;

        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        private LocalDateTime appliedTime;


        private BigDecimal elec;

    }

}
